#include "core_visualisation_flat.h"

#include <QDebug>

#include "core.h"

#include "common_defines_and_includes.h"

Core_Visualisation_Flat::Core_Visualisation_Flat(QWidget *parent) :
  #ifdef QCUSTOMPLOT_BASED
  QCustomPlot (parent),
  #endif
  _core         (nullptr),
  #ifdef QCUSTOMPLOT_BASED
  _graph        (this ->addGraph(this ->xAxis,
                                 this ->yAxis)),
  #endif
  _steps_count  (10000) {
#ifdef QCUSTOMPLOT_BASED
  for (int i = 0;
       i < 12;
       ++i) {
    _lines .append(addGraph(xAxis, yAxis));
  }
  for (int i = 0;
       i < 101;
       ++i) {
    _l .append(new QCPCurve (xAxis, yAxis));
    addPlottable(_l[i]);
  }
#endif
}

Core *Core_Visualisation_Flat::core(Core *in_new_value) {
  if (in_new_value != nullptr) {
    _core = in_new_value;

    connect((QObject*)(_core),
            SIGNAL(system_parameters_changed ()),
            this,
            SLOT  (update_visualisation      ()));

    update_visualisation();
  }

  return _core;
}

void Core_Visualisation_Flat::update_visualisation() {
#ifdef QCUSTOMPLOT_BASED
  Core::core_eqp_l_array lines = _core ->equipotential_lines (10, 10);
  int i = 0;
  double max_y = -1;
  QVector <QVector <double> > lx (101);
  QVector <QVector <double> > ly (101);
  for (auto arr : lines) {
    QVector <double> x;
    QVector <double> y;
    int j = 0;
    for (QPointF p : arr) {
      x .append(p .x());
      y .append(p .y());
      if (p .y() > max_y) {
        max_y = p .y();
      }
      lx[j] .append(p .x());
      ly[j] .append(p .y());
      ++j;
    }
    _lines [i] ->setData(x, y);
    ++i;
  }

  for (int i = 0;
       i < lx .size();
       ++i) {
    _l [i] ->setData(lx[i],ly[i]);
  }
  xAxis ->setRange(0., 2*M_PI);
  yAxis ->setRange(0.,max_y);

  replot();
#endif
}

void Core_Visualisation_Flat::resizeEvent(QResizeEvent *event) {
#ifdef QCUSTOMPLOT_BASED
  QCustomPlot::resizeEvent(event);
#endif
}
